package cn.xiaobai.j2exe.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.xiaobai.j2exe.MainApplication;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author yangdaji
 * @Date 2023/12/28
 * @PackageName cn.xiaobai.j2exe.utils
 * @ClassName FileUtils
 */
public class FileUtils {

    private final static Logger log = LoggerFactory.getLogger(MainApplication.class);

    /**
     * 转换路径
     *
     * @param path 系统路径
     * @return
     */
    public static String reversPath(String path) {
        if (StrUtil.isNotBlank(path)) {
            return path.replace("\\", "/");
        }
        return null;
    }

    /**
     * 转换路径
     *
     * @param file 文件
     * @return
     */
    public static String reversPath(File file) {
        if (file != null) {
            return file.getAbsolutePath().replace("\\", "/");
        }
        return null;
    }

    /**
     * 写入json 数据, 并保留格式
     *
     * @param jsonData   写入数据
     * @param outPath    保存位置
     * @param ignoreNull 是否忽略null 值, ture: 忽略属性为null的值
     */
    public static boolean writeFormatToJson(Object jsonData, String outPath, boolean ignoreNull) {
        return writeFormatToJson(jsonData, Paths.get(outPath).toFile(), ignoreNull);
    }

    /**
     * 写入json 数据, 并保留格式
     *
     * @param jsonData 写入数据
     * @param outPath  保存位置
     */
    public static boolean writeFormatToJson(Object jsonData, File outPath, boolean ignoreNull) {
        try {
            List<JSONWriter.Feature> features = new ArrayList<>();
            features.add(JSONWriter.Feature.PrettyFormat);
            if (!ignoreNull) {
                features.add(JSONWriter.Feature.WriteMapNullValue);
            }
            String jsonStr = JSON.toJSONString(jsonData, features.toArray(new JSONWriter.Feature[0]));
            IoUtil.write(new FileOutputStream(outPath), true, jsonStr.getBytes(StandardCharsets.UTF_8));
            return true;
        } catch (FileNotFoundException e) {
            log.error("write [ {} ] error: ", outPath, e.fillInStackTrace());
            return false;
        }
    }

    /**
     * 读取数据格式化为 json
     *
     * @param fileName 文件
     * @param objCls   目标类型
     * @param <T>
     * @return
     */
    public static <T> T readDataToJson(File fileName, Class<T> objCls) {
        if (fileName != null) {
            return readDataToJson(fileName.getAbsolutePath(), objCls);
        }
        return null;
    }

    /**
     * 读取数据格式化为 json
     *
     * @param fileName 文件
     * @param objCls   目标类型
     * @param <T>
     * @return
     */
    public static <T> T readDataToJson(String fileName, Class<T> objCls) {
        try {
            String readJson = IoUtil.read(new FileInputStream(fileName), StandardCharsets.UTF_8);
            if (JSONUtil.isTypeJSON(readJson)) {
                return JSONObject.parseObject(readJson, objCls);
            }
        } catch (Exception e) {
            log.error("read [ {} ] error: ", fileName, e.fillInStackTrace());
            return null;
        }
        return null;
    }


}
